home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / vtype.zip / VTYPE.C < prev    next >
Text File  |  1986-09-09  |  4KB  |  90 lines

  1.  
  2. /*** vtype filespec [t] ***  Source for DeSmet C, Rev. 1.7 */
  3.  
  4. #define CR '\015' /* code for carriage return (in octal) */
  5. #define ESC '\033'/* code for escape character (in octal) */
  6.  
  7. main(argc,argv)      /* Display a text file on the console with tabs expanded */
  8. int  argc;          /* to spaces.  Also, pause and restart on user keypress. */
  9. char *argv[];      /* The number of spaces per tab may be specified after   */
  10. {                  /* the filespec.  Example: vtype mprog.c 4 <enter>.  The */
  11.                   /* range is 1 to 20 spaces per tab and the default is 8. */
  12.                   /* Output may be aborted at any time by pressing [Esc].  */
  13.                   /* - PUBLIC DOMAIN software -  Vincent T. Bly  12/9/1983 */
  14.  
  15.     int   i, j, e, tab;
  16.     int   s_file, col_count;
  17.     char  c, key, buffer[256];
  18.  
  19.     if (argc < 2) {                    /* abort if no filespec */
  20.         puts("* Need filespec *\n");
  21.         exit(1);
  22.     }
  23.     s_file = fopen(argv[1], "r");
  24.     if (s_file == 0) {                /* abort if can't open file */
  25.         printf("* Cannot Open: %s *\n", argv[1]);
  26.         exit(1);
  27.     }
  28.     tab = 8;                        /* set default tab expansion to 8 */
  29.     if (argc > 2) {
  30.         i = atoi(argv[2]);
  31.         if ((i >= 1) && (i <= 20))    /* if filespec followed by a number, */
  32.             tab = i;                /* set tab expansion to "i" spaces/tab */
  33.     }
  34.     do {
  35.         e = fguts(buffer, s_file);        /* get a line from the file */
  36.         i = 0;                            /* (fguts equivalent to fgets) */
  37.         col_count = 0;
  38.         while ((c = buffer[i++]) != '\0') {
  39.             if (c == '\t') {            /* if character is a tab */
  40.                 for (j = 0; j < (tab - (col_count % tab)); j++)
  41.                     putchar(' ');        /* expand tab to "tab" spaces */
  42.                 col_count += j;            /* and update column count */
  43.             } else if (c != CR) {        /* else, if not a CR, */
  44.                 putchar(c);                /* print character and */
  45.                 ++col_count;            /* increment column count */
  46.             }
  47.         }
  48.         key = csts();                    /* check keyboard */
  49.         if (key == ESC)
  50.             break;                        /* if ESC key pressed, abort output */
  51.         else if (key != '\0') {            /* if any other key pressed, pause */
  52.             ci();                        /* remove keystroke from buffer */
  53.             key = ci();                    /* wait for next keystroke */
  54.             if (key == ESC)
  55.                 break;                    /* if ESC key pressed, abort output */
  56.         }
  57.     } while (e != -1);                    /* loop until end of file */
  58.     close(s_file);                        /* close it & return to DOS */
  59. }
  60.  
  61. fguts(buffer, file)        /* This is a replacement for fgets() for DeSmet C, */
  62. char  buffer[];            /* version 1.7 that DOES return -1 for EOF.  Later */ 
  63. int      file;                /* revisions of DeSmet C and most other compilers  */ 
  64. {                        /* can eliminate this function and use fgets().    */
  65.     int  i = 0;
  66.     int     j = 0;
  67.  
  68.     while ((j = getc(file)) != -1) { /* set j to file char & check for EOF */
  69.         buffer[i++] = j;             /* put char in buffer & bump pointer */
  70.         if (j == 10)                 /* break loop if char was a linefeed */
  71.             break;                     /* (indicates end of current line) */
  72.     }
  73.     buffer[i] = '\0';                 /* terminate with end-of-string marker */
  74.     return(j);                         /* return value of last char read */
  75. }
  76.  
  77. /* NOTES:
  78.      The csts() function returns the scan code from a keyboard input if a
  79.  key has been pressed.  It returns '\0' if no key has been pressed.  It does
  80.  not remove the keystroke from the keyboard buffer.
  81.      The ci() function returns the scan code from a keyboard input, but waits
  82.  if no key has been pressed.  It does remove the keystroke from the buffer.
  83.      The main do loop expands tabs and prints all other characters--except 
  84.  carriage returns.  The carriage return should not be printed, since putchar()
  85.  will expand the following line feed into a carriage return/line feed.
  86.      When redirecting the output of VTYPE to a disk file under MS-DOS 2.0,
  87.  the End-OF-File termination will be slightly abnormal.  Some text handling
  88.  programs, such as VEDIT, may display a string of "^@"s at the end of the
  89.  file.  If you encounter this, simply delete the string of "^@"s and re-save
  90.  the file. */